using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Assets.Scripts.UI;

public class CircleBtnPanel : BasePanel
{
    public Color normalColor;
    public Color highLightedColor;
    public Color selectedColor;
    public List<CircleBtnImg> circleBtnImgList;
    public TextMeshProUGUI btnText;
    public PlayerController _playerController;
    public PlayerController playerController
    {
        get
        {
            if (_playerController == null)
            {
                _playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
            }
            return _playerController;
        }
        set
        {
            _playerController = value;
        }
    }
    public PlayerWeaponController _playerWeaponController;
    public PlayerWeaponController playerWeaponController
    {
        get
        {
            if (_playerWeaponController == null)
            {
                _playerWeaponController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerWeaponController>();
            }
            return _playerWeaponController;
        }
        set
        {
            _playerWeaponController = value;
        }
    }
    public void Init()
    {
        foreach (var item in circleBtnImgList)
        {
            item.Init(this);
        }
    }
    private void Start()
    {

    }
    private void Update()
    {
        bool isHigned = false;
        foreach (var circleBtnImg in circleBtnImgList)
        {
            if (!circleBtnImg.gameObject.activeInHierarchy) continue;
            isHigned = isHigned || circleBtnImg.CheckIsHighed();
        }
        if(!isHigned)
        {
            ShowBtnDesc("");
        }
    }
    public override void OnEnter()
    {
        GameManager.Instance.TranslateGameState(GameState.None);
        base.OnEnter();
        Init();
        ShowBtnDesc("");
        CircleUpdate(playerWeaponController.GetWeaponList());
    }
    public override void OnExit()
    {
        base.OnExit();
        ShowBtnDesc("");
        GameManager.Instance.TranslateGameState(GameState.Gaming);
    }
    public void CircleUpdate(List<ComponentManager> weaponList)
    {
        for (int i = 0; i < weaponList.Count; i++)
        {
            circleBtnImgList[i].OnEnable_(weaponList[i], i == playerWeaponController.curWeaponIndex ? selectedColor : normalColor, highLightedColor);
        }
        for (int i = weaponList.Count; i < circleBtnImgList.Count; i++)
        {
            circleBtnImgList[i].OnDisable_();
        }
    }
    public void CircleSwitch(int number)
    {
        playerWeaponController.SwitchWeapon(number);
        if (UIManager.Instance.GetTopPanel().GetPanelType() == UIPanelType.CircleBtnPanel)
        {
            UIManager.Instance.PopPanel();
        }
    }
    public void ShowBtnDesc(string desc)
    {
        btnText.text = desc;
    }
}
